package com.ruoyi.web.controller.system;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JingfeiKaifang;
import com.ruoyi.system.service.IJingfeiKaifangService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-07-10
 */
@Controller
@RequestMapping("/system/kaifang")
public class JingfeiKaifangController extends BaseController
{
    private String prefix = "system/kaifang";

    @Autowired
    private IJingfeiKaifangService jingfeiKaifangService;

    @RequiresPermissions("system:kaifang:view")
    @GetMapping()
    public String kaifang()
    {
        return prefix + "/kaifang";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:kaifang:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiKaifang jingfeiKaifang)
    {
        startPage();
        List<JingfeiKaifang> list = jingfeiKaifangService.selectJingfeiKaifangList(jingfeiKaifang);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:kaifang:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiKaifang jingfeiKaifang)
    {
        List<JingfeiKaifang> list = jingfeiKaifangService.selectJingfeiKaifangList(jingfeiKaifang);
        ExcelUtil<JingfeiKaifang> util = new ExcelUtil<JingfeiKaifang>(JingfeiKaifang.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:kaifang:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiKaifang jingfeiKaifang)
    {
        return toAjax(jingfeiKaifangService.insertJingfeiKaifang(jingfeiKaifang));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:kaifang:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JingfeiKaifang jingfeiKaifang = jingfeiKaifangService.selectJingfeiKaifangById(id);
        mmap.put("jingfeiKaifang", jingfeiKaifang);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:kaifang:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiKaifang jingfeiKaifang)
    {
        return toAjax(jingfeiKaifangService.updateJingfeiKaifang(jingfeiKaifang));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:kaifang:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiKaifangService.deleteJingfeiKaifangByIds(ids));
    }

    @PostMapping("shezhi")
    @ResponseBody
    public JsonResult<?> shezhi(@RequestParam("deptid")String ppDeptId,@RequestParam("jingfeipiciid")String ppJingfeipiciid,@RequestParam("jiezhishijian")String ppJiezhishijian){
        try {
            JingfeiKaifang mmJingfeikaifang = new JingfeiKaifang();
            mmJingfeikaifang.setDeptid(ppDeptId);
            mmJingfeikaifang.setJingfeipiciid(ppJingfeipiciid);

            SimpleDateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
            LocalDateTime ldt = dfm.parse(ppJiezhishijian).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusDays(1).minusSeconds(1);
            Date date = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());

            List<JingfeiKaifang> mmList = jingfeiKaifangService.selectJingfeiKaifangList(mmJingfeikaifang);

            if (mmList.size()==1) {
                mmJingfeikaifang = mmList.get(0);
                mmJingfeikaifang.setJiezhishijian(date);
                int modify = jingfeiKaifangService.updateJingfeiKaifang(mmJingfeikaifang);
                return modify == 1 ? JsonResult.getSuccessResult("设置成功") : JsonResult.getErrorResult("设置失败");
            }else if (mmList.size()==0){
                mmJingfeikaifang.setId(UUID.randomUUID().toString());
                mmJingfeikaifang.setIsvalid((long)1);
                mmJingfeikaifang.setJiezhishijian(date);
                int add = jingfeiKaifangService.insertJingfeiKaifang(mmJingfeikaifang);
                return add == 1 ? JsonResult.getSuccessResult("设置成功") : JsonResult.getErrorResult("设置失败");
            }else{
                return JsonResult.getErrorResult("设置出错");
            }
        } catch (Exception e) {
            logger.error("kaifang -> shezhi: "+e.getMessage());
            return JsonResult.getErrorResult(e.getMessage());
        }
    }

    @PostMapping("findone")
    @ResponseBody
    public JsonResult<?> findone(@RequestParam("deptid")String ppDeptId,@RequestParam("jingfeipiciid")String ppJingfeipiciid){
        try {
            JingfeiKaifang mmJingfeikaifang = new JingfeiKaifang();
            mmJingfeikaifang.setDeptid(ppDeptId);
            mmJingfeikaifang.setJingfeipiciid(ppJingfeipiciid);

            List<JingfeiKaifang> mmList = jingfeiKaifangService.selectJingfeiKaifangList(mmJingfeikaifang);

            if (mmList.size()==1) {
                mmJingfeikaifang = mmList.get(0);
                return JsonResult.getSuccessResult(mmJingfeikaifang);
            }else if (mmList.size()==0){
                return JsonResult.getSuccessResult(mmJingfeikaifang);
            }else{
                return JsonResult.getErrorResult("查询出错");
            }
        } catch (Exception e) {
            logger.error("kaifang -> findone: "+e.getMessage());
            return JsonResult.getErrorResult(e.getMessage());
        }
    }
}
